package cn.swust.designpatternlearningplatform.controller;


import cn.swust.designpatternlearningplatform.common.AnnotationUtils.Authentication;
import cn.swust.designpatternlearningplatform.common.AnnotationUtils.NotNull;
import cn.swust.designpatternlearningplatform.common.Result.R;
import cn.swust.designpatternlearningplatform.common.utils.AuthUserContext;
import cn.swust.designpatternlearningplatform.domain.dto.StudentAllTypeTopicNumberDto;
import cn.swust.designpatternlearningplatform.domain.dto.StudentPersonInformationDto;
import cn.swust.designpatternlearningplatform.domain.dto.StudentPieChartDataDto;
import cn.swust.designpatternlearningplatform.domain.enums.RoleEnum;
import cn.swust.designpatternlearningplatform.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


import java.io.IOException;
import java.time.LocalDate;

import java.util.*;

@RestController
@Slf4j
@RequestMapping("/student")
@Authentication(RoleEnum.STUDENT)
public class StudentController {
    @Resource
    private StudentService studentService;


    /**
     * @return R<Object>
     * @author wang
     * description 通过登录的学号获取学生的基本个人信息展示再个人主页界面
     * date 2023/4/16 18:22
     */
    @GetMapping("/getInfo")
    public R<Object> selectStudentPersonal() {
        String studentNumber = AuthUserContext.get().getNumber();
        System.out.println("********************");
        System.out.println(studentNumber);
        log.info(studentNumber);
        StudentPersonInformationDto studentPersonInformationDto = studentService.getStudentInformation(studentNumber);
        return R.success(studentPersonInformationDto);
    }

    //获取做题信息
    @GetMapping("/getUserTopic")
    public R<Object> getQuantity() {
        String studentNumber = AuthUserContext.get().getNumber();
        StudentAllTypeTopicNumberDto studentAllTypeTopicNumberDto = studentService.getAllTypeTopicNumber(studentNumber);
        return R.success(studentAllTypeTopicNumberDto);
    }

    /**
     * @param url:
     * @return R<Object>
     * @author wang
     * description 上传头像
     * date 2023/4/16 20:53
     */
    @PostMapping("/uploadAvatar")
    @NotNull
    public R<Object> UploadHead(@RequestParam("url") String url) {
        System.out.println(url);
        String studentNumber = AuthUserContext.get().getNumber();
        String msg = studentService.uploadHead(studentNumber, url);
        return R.success(msg);
    }

    /**
     * @return R<String>
     * @author wang
     * description 获取头像地址
     * date 2023/4/17 19:00
     */
    @GetMapping("/getAvatar")
    public R<Object> GetHead() {
        String studentNumber = AuthUserContext.get().getNumber();
        Object avatar_src = studentService.getHeadPortrait(studentNumber);
        return R.success(avatar_src);
    }

    /**
     * @return R<Object>
     * @author wang
     * description 获取日历图数据，时间加做题数量
     * date 2023/4/17 16:14
     */
    @GetMapping("/getTimeStamp")
    public R<Object> GetNum() {
        String studentNumber = AuthUserContext.get().getNumber();
        Map<LocalDate, Long> map = studentService.QueryNumber(studentNumber);
        return R.success(map);
    }

    /**
     * @return R<Object>
     * @author wang
     * description 获取正确错误的做题数量，返回前端形成饼图
     * date 2023/4/17 19:18
     */
    @GetMapping("/getPie")

    public R<Object> Compare() {
        String studentNumber = AuthUserContext.get().getNumber();
        StudentPieChartDataDto studentPieChartDataDto = new StudentPieChartDataDto();
        studentPieChartDataDto = studentService.getPieChartData(studentNumber);
        return R.success(studentPieChartDataDto);
    }
}


